﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 熄灯
{
    public class Solution
    {
        public int[,] light;
        public int row;
        public int col;

        /// <summary>
        /// 默认row>=col
        /// </summary>
        /// <param name="light"></param>
        public Solution(int[,] light)
        {
            this.light = light;
            row = light.GetLength(0);
            col = light.Length / row;
        }

        public int Solve()
        {
            int max = (int)Math.Pow(2, col);

            for (int i = 0; i < max; i++)
            {
                int[,] map = (int[,])light.Clone();
                for (int j = 0; j < col; j++)
                {
                    int num = (i >> j) & 1;
                    if (num == 1)
                    {
                        Change(map, 0, j);
                    }
                }
                bool flag = Check(map);
                if (flag)
                {
                    return i;
                }
            }

            return -1;
        }

        public void Change(int[,] map, int i, int j)
        {
            map[i, j] = 1 - map[i, j];
            if (i >= 1)
            {
                map[i - 1, j] = 1 - map[i - 1, j];
            }
            if (j >= 1)
            {
                map[i, j - 1] = 1 - map[i, j - 1];
            }
            if (i + 1 < row)
            {
                map[i + 1, j] = 1 - map[i + 1, j];
            }
            if (j + 1 < col)
            {
                map[i, j + 1] = 1 - map[i, j + 1];
            }
        }

        public bool Check(int[,] map)
        {
            for (int i = 1; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    if (map[i - 1, j] == 1)
                    {
                        Change(map, i, j);
                    }
                }
            }

            for (int j = 0; j < col; j++)
            {
                if (map[row - 1, j] == 1)
                {
                    return false;
                }
            }
            return true;
        }

        public void Print(int result)
        {
            if (result == -1)
            {
                Console.WriteLine("无解");
                return;
            }
            int[,] op = new int[row, col];
            int[,] map = (int[,])light.Clone();
            for(int i = 0; i < col; i++)
            {
                op[0, i] = (result >> i) & 1;
                if (op[0, i] == 1)
                {
                    Change(map, 0, i);
                }
            }

            for (int i = 1; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    if (map[i - 1, j] == 1)
                    {
                        op[i, j] = 1;
                        Change(map, i, j);
                    }
                }
            }

            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    Console.Write(op[i,j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
